import React from 'react'
import { withRouter } from 'react-router-dom'

import './style.css'

const AddressItem = ({ 
    history, data = {}, position = 'edit', 
    hideBtn = false, chooseAddress
}) => {
    const toEdit = () => {
        if (position === 'order') {
            history.push(`/addressChoose`)
        } else if (position === 'edit') {
            history.push(`/addressEdit?id=${data._id}`)
        }
    }
    const choose = () => {
        chooseAddress && chooseAddress(data)
    }
    
    return (
        <div 
            className='addressItem-wrap'
            onClick={choose}
        >
            <ul className='addressItem' >
                <li className='names'>{data.name}, {data.phone}</li>
                <li className='address' >{data.province}{data.detail}</li>
            </ul>
            {
                !hideBtn && (
                    <div
                        className='addressItem-edit'
                        onClick={toEdit}
                    >
                        { position === 'edit' && '编辑' }
                        { position === 'order' && '更换' }
                    </div>
                )
            }
        </div>
    )
}

export default withRouter(AddressItem)